package com.kk.mediator.smarthouse;

import java.util.HashMap;

/**
 * 具体的中介者对象
 *
 * @author o_o
 * @date 2022-06-16
 */
public class CurrentMediator extends Mediator {

    private HashMap<String, Colleague> colleagueMap;
    private HashMap<String, String> interMap;

    public CurrentMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);

        if (colleague instanceof AlarmClock) {
            interMap.put("AlarmClock", colleagueName);
        }
        if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        }

    }


    @Override
    public void getMessage(int stateChange, String colleagueName) {
        if (colleagueMap.get(colleagueName) instanceof TV) {
            if (stateChange == 1) {
                ((TV) colleagueMap.get(colleagueName)).startTV();
            }
            if (stateChange == 0) {
                ((TV) colleagueMap.get(colleagueName)).stopTV();
            }
        }

        if (colleagueMap.get(colleagueName) instanceof AlarmClock) {
            if (stateChange == 1) {
                ((AlarmClock) colleagueMap.get(colleagueName)).startAlarmClock();
            }
            if (stateChange == 0) {
                ((AlarmClock) colleagueMap.get(colleagueName)).stopAlarmClock();
            }
        }
    }

}
